/*
 * @Author: jiangyg email@jiangyg.com
 * @Date: 2022-11-12 05:49:58
 * @LastEditors: jiangyg email@jiangyg.com
 * @LastEditTime: 2022-12-10 21:48:12
 * @FilePath: /frontend/mall-frontend/src/utils/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

/**
 * 获取uuid
 */
export function UUID() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
    return (c === "x" ? (Math.random() * 16) | 0 : "r&0x3" | "0x8").toString(16);
  });
}

/**
 * URL地址
 * @param {*} s
 */
export function isURL(s) {
  return /^http[s]?:\/\/.*/.test(s);
}

/**
 * 指定元素追加 class
 * @param {*} ele 元素
 * @param {*} nclazz 待追加的class
 * @returns
 */
export function addElementClassName(ele, nclazz) {
  if (!ele) {
    return;
  }
  let clzs = [];
  if (ele.className) {
    let os = ele.className.split(" ");
    for (let i = 0; i < os.length; i++) {
      let clazz = os[i].trim();
      if (clzs.indexOf(clazz) != -1) {
        continue;
      }
      clzs.push(clazz);
    }
  }
  if (nclazz && clzs.indexOf(nclazz) == -1) {
    clzs.push(nclazz);
  }
  ele.className = clzs.join(" ");
}

/**
 * 指定元素移除class
 * @param {*} ele 元素
 * @param {*} rclazz 待移除class
 * @returns
 */
export function removeElementClassName(ele, rclazz) {
  if (!ele) {
    return;
  }
  let clzs = [];
  if (ele.className) {
    let os = ele.className.split(" ");
    for (let i = 0; i < os.length; i++) {
      let clazz = os[i].trim();
      if (clzs.indexOf(clazz) != -1 || clazz === rclazz) {
        continue;
      }
      clzs.push(clazz);
    }
  }
  ele.className = clzs.join(" ");
}

/**
 * 人民币格式化
 * @param {*} val 待格式化数值
 * @returns 格式化后内容
 */
export function amountCNYFormatter(val) {
  if (val === null || val === undefined) {
    return '';
  }
  const options = { style: 'currency', currency: 'CNY', };
  return val.toLocaleString('zh-CN', options);
}